package com.example.springboot.service;

import com.example.springboot.dto.PostDTO;
import com.example.springboot.entity.ForumPost;
import com.example.springboot.mapper.ForumPostMapper;
import com.github.pagehelper.Page;  // 使用 PageHelper 的 Page
import com.github.pagehelper.PageHelper;  // 引入 PageHelper
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ForumPostService {

    @Autowired
    private ForumPostMapper forumPostMapper;

    private static final Logger logger = LoggerFactory.getLogger(ForumPostService.class);

    public Integer createPost(ForumPost post) {
        forumPostMapper.insertPost(post);
        return post.getPostId(); // 确保返回插入后的 postId
    }

    public PostDTO getPostById(Integer postId) {
        ForumPost post = forumPostMapper.selectPostById(postId);
        if (post == null) {
            return null;
        }
        PostDTO postDTO = new PostDTO();
        postDTO.setPostId(post.getPostId());
        postDTO.setUserId(post.getUserId());
        postDTO.setTitle(post.getTitle());
        postDTO.setContent(post.getContent());
        postDTO.setCreatedAt(post.getCreatedAt().toString());
        postDTO.setUpdatedAt(post.getUpdatedAt().toString());
        postDTO.setIsDeleted(post.getIsDeleted());
        postDTO.setViewCount(post.getViewCount());
        postDTO.setImageUrls(forumPostMapper.getImageUrls(postId));
        postDTO.setLikeCount(forumPostMapper.getLikeCount(postId));
        postDTO.setCommentCount(forumPostMapper.getCommentCount(postId));
        return postDTO;
    }

    public List<ForumPost> getPostsByUserId(Integer userId) {
        return forumPostMapper.selectPostsByUserId(userId);
    }

    public void updatePost(Integer postId, ForumPost post) {
        // 设置 postId 以确保更新正确的记录
        post.setPostId(postId);
        forumPostMapper.updatePost(postId, post);
    }

    public void deletePost(Integer postId) {
        forumPostMapper.deletePost(postId);
    }

    public List<ForumPost> getAllPosts() {
        return forumPostMapper.selectAllPosts();
    }

    public Page<ForumPost> getAllPostsForAdmin(String username, int page, int size, String query) {
        if (!hasSuperRole(username)) {
            logger.warn("User {} has no super admin privileges", username);
            return null;
        }
        // 使用 PageHelper 进行分页
        PageHelper.startPage(page, size, "created_at DESC");  // PageHelper 页码从 1 开始
        if (query != null && !query.trim().isEmpty()) {
            return forumPostMapper.findByQuery(query);
        }
        return forumPostMapper.findAllPosts();
    }

    public boolean deletePostAdmin(Integer postId, String username) {
        if (!hasSuperRole(username)) {
            logger.warn("User {} has no super admin privileges", username);
            return false;
        }
        forumPostMapper.delete(postId);
        return true;
    }

    private boolean hasSuperRole(String username) {
        if (username == null) {
            return false;
        }
        String role = forumPostMapper.findRoleByUsername(username);
        return "super".equals(role);
    }
}